package com.cxd.web.backend;

import com.cxd.entity.EbOrder;
import com.cxd.entity.EbUser;
import com.cxd.service.IEbOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/order")     //工号
public class OrderController {
    @Autowired  //spring帮我们将NewsService的对象创建出来
    private IEbOrderService orderService;
    /**
     * 获取分页新闻信息
     * @return

    /**
     * model 将web层的数据传递jsp页面
     */
    @RequestMapping("/list")
    public String list(HttpSession session, Model model,
                       @RequestParam(value = "page", defaultValue = "1")int psize,
                       @RequestParam(value = "pageSize",defaultValue = "3")int pageSize){
        Object user = session.getAttribute("lgnUser");
        if (user == null){
            return "redirect:/f-login/to_login";
        }
        EbUser ebUser = (EbUser) user;
        if(ebUser.getEuStatus()!=2){
            return "redirect:/f-index/index";
        }
        model.addAttribute("user",ebUser);

        System.out.println("receive request!!;psize==>"+psize);
        List<EbOrder> orderList = orderService.listByPage(psize,3);
        final Integer totalPage = orderService.findTotalPage(pageSize);

        model.addAttribute("orderList",orderList);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("currPage",psize);
        model.addAttribute("tag",4);


        return "manage/order";//跳转到/manage/news.jsp 将数据显示出来
    }


    @RequestMapping("/to_edit")
    public String toEdit(HttpSession session,Model model, @RequestParam("eoId") int eoId) {
        Object user = session.getAttribute("lgnUser");
        if (user == null){
            return "redirect:/f-login/to_login";
        }
        EbUser ebUser = (EbUser) user;
        if(ebUser.getEuStatus()!=2){
            return "redirect:/f-index/index";
        }
        model.addAttribute("user",ebUser);
        System.out.println("点击修改跳过来了，eoId=" + eoId);
        EbOrder ebOrder = orderService.selectOne(eoId);
        model.addAttribute("ebOrder", ebOrder);
        model.addAttribute("tag",4);
        return "manage/order-modify";
    }

    @RequestMapping("/edit")
    public String edit(HttpSession session,EbOrder ebOrder,Model model){
        Object user = session.getAttribute("lgnUser");
        if (user == null){
            return "redirect:/f-login/to_login";
        }
        EbUser ebUser = (EbUser) user;
        if(ebUser.getEuStatus()!=2){
            return "redirect:/f-index/index";
        }
        model.addAttribute("user",ebUser);
        System.out.println("ebOrder==>"+ebOrder);
        Integer result = orderService.updateOrder(ebOrder);
        model.addAttribute("tag",4);
        model.addAttribute("result",result);
        return "manage/manage-result";
    }

    @RequestMapping("/delete")
    public String delete(HttpSession session,Integer id,Model model){
        Object user = session.getAttribute("lgnUser");
        if (user == null){
            return "redirect:/f-login/to_login";
        }
        EbUser ebUser = (EbUser) user;
        if(ebUser.getEuStatus()!=2){
            return "redirect:/f-index/index";
        }
        model.addAttribute("user",ebUser);
        int result = orderService.deleteByPk(id);
        model.addAttribute("tag",4);
        model.addAttribute("result",result);
        return "manage/manage-result";
    }
    @RequestMapping("/search")
    public String search(HttpSession session,Model model,int eoId,String eoUserId){
        Object user = session.getAttribute("lgnUser");
        if (user == null){
            return "redirect:/f-login/to_login";
        }
        EbUser ebUser = (EbUser) user;
        if(ebUser.getEuStatus()!=2){
            return "redirect:/f-index/index";
        }
        model.addAttribute("user",ebUser);
        if (eoId!=0){
            EbOrder ebOrder = orderService.selectOne(eoId);
            model.addAttribute("orderList",ebOrder);
            model.addAttribute("tag",4);


            return "manage/order";//跳转到/manage/news.jsp 将数据显示出来
        }else if (eoUserId.trim().length() != 0){
            List<EbOrder> ebOrders = orderService.findByUid(eoUserId);
            model.addAttribute("orderList",ebOrders);
            model.addAttribute("tag",4);


            return "manage/order";//跳转到/manage/news.jsp 将数据显示出来
        }
        return "manage/order";
    }
}
